package com.qianbei.share.wx;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;

import com.qianbei.BuildConfig;
import com.qianbei.R;
import com.qianbei.common.base.BaseApplication;
import com.qianbei.common.net.control.Constant;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXTextObject;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.io.ByteArrayOutputStream;
import java.util.List;

public class WxShare {

    // IWXAPI 是第三方app和微信通信的openapi接口
    public static IWXAPI api;
    public static String APP_ID = BuildConfig.WE_CHAT_APP_ID;//
//			"wxbc2e28febcfef84f";// 北京

    private static WxShare wxShare;

    public static WxShare getInstent() {
        if (wxShare == null) {
            wxShare = new WxShare();
        }
        return wxShare;
    }

    public WxShare() {

        api = WXAPIFactory.createWXAPI(BaseApplication.mApplication, APP_ID,
                false);
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api.registerApp(APP_ID);
    }

    /**
     * 分享文字 到 朋友圈
     */
    public void ShareText(String text) {
        WXTextObject textObj = new WXTextObject();
        textObj.text = text;

        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = textObj;
        msg.description = text;

        // ππ‘Ï“ª∏ˆReq
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("text"); // transaction◊÷∂Œ”√”⁄Œ®“ª±Í ∂“ª∏ˆ«Î«Û
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;// :
        // SendMessageToWX.Req.WXSceneSession;

        // µ˜”√apiΩ”ø⁄∑¢ÀÕ ˝æ›µΩŒ¢–≈
        api.sendReq(req);
    }

    /**
     * url 分享到朋友圈
     *
     * @param url
     * @param title
     * @param description
     * @param isPy
     */
    public void shareURl(final String url, final String title,
                         final String description, boolean isPy) {

        // Thread t =new Thread(new Runnable() {
        // public void run() {
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;

        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = "【前辈】" + description;// "WebPage Description WebPage Description WebPage Description WebPage Description WebPage Description WebPage Description WebPage Description WebPage Description WebPage Description Very Long Very Long Very Long Very Long Very Long Very Long Very Long";
        Bitmap thumb1 = BitmapFactory.decodeResource(
                BaseApplication.mApplication.getResources(),
                R.drawable.iv_logo);
        msg.thumbData = bmpToByteArray(thumb1, true);
        //
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message = msg;
        req.scene = isPy ? SendMessageToWX.Req.WXSceneTimeline
                : SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);

        // }
        // });
        // t.start();

    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis())
                : type + System.currentTimeMillis();
    }

    public static byte[] bmpToByteArray(final Bitmap bmp,
                                        final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 检查是否安装微信
     *
     * @param context
     * @return
     */
    public static boolean isWeixinAvilible() {
        final PackageManager packageManager = BaseApplication.mApplication.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }

        return false;
    }

}
